package com.JZ.QM.utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * @author cyc
 * @email cyc@gmail.com
 * @date 2020/6/24 11:37
 */
public class RiskLevel {
    /**
     * 读取风险评分表json文件为字符串类型
     * @return 将json评分卡返回成String形式
     */
    public static String readJson(){
        String jsonStr = "";
        try {
            InputStream inputStream= new ClassPathResource("riskValue.json").getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = br.read()) != -1) {
                sb.append((char) ch);
            }
            br.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            System.out.println("文件读取异常");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将json文件读取至缓存中
     * @return 返回json对象
     */
    public static JSONObject getJson() throws JSONException {
        JSONObject json=new JSONObject(readJson());
        System.out.println(json);
        return json;
    }

    /**
     * 对用户的得分来进行风险评级
     * @param riskScore 风险测试获得的分数
     * @return 测评分数对应的风险等级
     */
    public static int getRiskLevel(int riskScore){
        if (riskScore <= 20){
            return 1;
        } else if (20 < riskScore && riskScore <= 40) {
            return 2;
        } else if (41 < riskScore && riskScore <=60){
            return 3;
        } else if (61 < riskScore && riskScore <=80){
            return 4;
        } else {
            return 5;
        }
    }
}
